<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2020/7/3 0003
 * Time: 09:47
 */

namespace app\currency\service;

use think\Db;

/**
 * 订单收益
 * Class OrderRoyalty
 * @package app\currency\service
 */
class OrderRoyalty
{
    /**
     * 根据条件返回收益金额总数
     * @param int $cid 收益获得人ID
     * @param string $modal 绑定收益'bind' 服务收益'service' 所有'all'
     * @param string $range 收益分组（all 全部 self自己 team团队）
     * @param string $start_at 开始时间Y-m-d H:i:s
     * @param string $end_at   结束时间Y-m-d H:i:s
     * @return int 收益金额
     */
    public static function getRoyaltyPrice($cid, $modal = 'all', $range = 'all', $start_at = '', $end_at = ''){
        $where = self::setConditions($cid, $modal, $range, $start_at, $end_at);
        if(empty($where))
        {
            return 0;
        }
        $sql = "SELECT SUM(amount) AS total_amount FROM clean_order_royalty WHERE ".$where;
        $result = Db::query($sql);
        if($result !== false)
        {
            return $result[0]['total_amount']+0;
        } else {
            return 0;
        }
    }

    /**
     * 根据天分组返回收益列表
     * @param int $cid 保洁员ID
     * @param string $modal 收益类型（all 全部 service服务 bind绑定）
     * @param string $range 收益分组（all 全部 self自己 team团队）
     * @param string $start_at 开始时间 Y-m-d H:i:s
     * @param string $end_at 结束时间 Y-m-d H:i:s
     * @return mixed
     */
    public static function getRoyaltyListByDay($cid, $modal = 'all', $range = 'all',$start_at = '', $end_at = '')
    {
        $where = self::setConditions($cid, $modal, $range, $start_at, $end_at);
        if(empty($where))
        {
            return [];
        }
        $group_sql = "SELECT DATE_FORMAT(create_at,'%Y-%m-%d') AS day,GROUP_CONCAT(id) AS ids FROM clean_order_royalty WHERE ".$where." GROUP BY DATE_FORMAT(create_at,'%Y-%m-%d') ORDER BY DATE_FORMAT(create_at,'%Y-%m-%d') DESC";
        $result = Db::query($group_sql);
        if($result !== false)
        {
            return $result;
        } else {
            return [];
        }
    }

    /**
     * 返回查询条件
     * @param int $cid 保洁员ID
     * @param string $modal 收益类型（all 全部 service服务 bind绑定）
     * @param string $range 收益分组（all 全部 self自己 team团队）
     * @param string $start_at 开始时间 Y-m-d H:i:s
     * @param string $end_at 结束时间 Y-m-d H:i:s
     * @return array|string
     */
    public static function setConditions($cid,$modal,$range,$start_at, $end_at)
    {
        $team_ids = CleanerService::getTeamIds($cid,2);//获取所有团队的ID集合
        if($range == 'all')
        {
            $ids = array_merge($team_ids,[$cid]);
        } elseif ($range == 'self')
        {
            $ids = [$cid];
        } else {
            $ids = $team_ids;
        }
        if(empty($ids))
        {
            return [];
        }
        if(count($ids) === 1)
        {
            $where = "cid = {$cid} AND ccid = {$ids[0]}";
        } else {
            $ids = implode(',',$ids);
            $where = "cid = {$cid} AND ccid IN ({$ids})";
        }
        if($modal !== 'all')
        {
            $where .= " AND modal = '{$modal}'";
        }
        if($start_at != '' || $end_at != '')
        {
            if(DateService::checkDateTime($start_at) !== false && DateService::checkDateTime($end_at) !== false)
            {
                $where .= " AND create_at BETWEEN '{$start_at}' AND '{$end_at}'";
            }
        }
        return $where;
    }
}